using MaterialDesignColors;
using MyToDo.Service;
using MyToDo.Shared.Dtos;
using MyToDo.Shared.Parameters;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Data;

namespace MyToDo.ViewModels
{
    /// <summary>
    /// 附加属性演示视图的ViewModel
    /// 提供用于数据绑定的属性和方法
    /// </summary>
    public class AttachedPropertyDemoViewModel : BindableBase
    {
        private readonly IMemoService memoService;

        private bool isItemSelected;
        private int maxTextLength = 20;
        private string inputText;

        /// <summary>
        /// 备忘录数据集合
        /// </summary>
        public ObservableCollection<MemoDto> MemoDtos { get; private set; }

        /// <summary>
        /// 是否选中项的状态
        /// 用于IsSelected附加属性的绑定演示
        /// </summary>
        public bool IsItemSelected
        {
            get { return isItemSelected; }
            set { SetProperty(ref isItemSelected, value); }
        }

        /// <summary>
        /// 文本最大长度
        /// 用于MaxLength附加属性的绑定演示
        /// </summary>
        public int MaxTextLength
        {
            get { return maxTextLength; }
            set { SetProperty(ref maxTextLength, value); }
        }

        /// <summary>
        /// 输入文本
        /// 用于MaxLength附加属性的绑定演示
        /// </summary>
        public string InputText
        {
            get { return inputText; }
            set { SetProperty(ref inputText, value); }
        }

        public AttachedPropertyDemoViewModel(IMemoService memoService)
        {
            this.memoService = memoService;
            MemoDtos = new ObservableCollection<MemoDto>();
            
            // 初始化数据
            InitializeData();
        }

        /// <summary>
        /// 初始化演示数据
        /// </summary>
        private async void InitializeData()
        {

            var todoResult = await memoService.GetAllAsync(new QueryParameter()
            {
                PageIndex = 0,
                PageSize = 100,
            });

            if (todoResult.Status)
            {
                MemoDtos.Clear();
                foreach (var item in todoResult.Result.Items)
                {
                    MemoDtos.Add(item);
                }
            }

            // 如果没有数据，添加一些模拟数据用于演示
            if (MemoDtos.Count == 0)
            {
                AddMockData();
            }
        }

        /// <summary>
        /// 添加模拟数据用于演示
        /// </summary>
        private void AddMockData()
        {
            MemoDtos.Add(new MemoDto
            {
                Id = 1,
                Title = "演示备忘录1",
                Content = "这是第一条用于附加属性演示的备忘录数据",
            });

            MemoDtos.Add(new MemoDto
            {
                Id = 2,
                Title = "演示备忘录2",
                Content = "这是第二条用于附加属性演示的备忘录数据",
            });

            MemoDtos.Add(new MemoDto
            {
                Id = 3,
                Title = "演示备忘录3",
                Content = "这是第三条用于附加属性演示的备忘录数据",
            });
        }
    }
}